<template>
  <page-header-wrapper>
    <a-card class="search-card  m-b-20" :bordered="false">
      <div class="table-page-search-wrapper">
        <a-form layout="inline">
          <a-row :gutter="48">
            <a-col :md="14" :lg="14" :xl="12" :xxl="8" :sm="24">
              <a-form-item label="部门名称">
                <a-input v-model="queryParam.DepartmentName" placeholder="请输入部门名称" @pressEnter="$refs.table.refresh(true)"/>
              </a-form-item>
            </a-col>
            <a-col :md="10" :lg="10" :xl="8" :xxl="6" :sm="24">
              <div class="table-page-search-submitButtons">
                <a-button type="primary" @click="$refs.table.refresh(true)">查询</a-button>
                <a-button style="margin-left: 8px" @click="handleReset">重置</a-button>
              </div>
            </a-col>
          </a-row>
        </a-form>
      </div>
    </a-card>
    <a-card :bordered="false">
      <s-table
        ref="table"
        :scroll="{x: true}"
        :rowKey="record => record.ID"
        :columns="columns"
        :data="loadData"
        :pagination="{ showTotal: total => `共 ${total} 条` }"
      >
        <!-- 表格操作栏 -->
        <template slot="tabAction">
          <a-button class="m-r-10" type="primary" icon="plus" @click="handleOperation('add')">新增部门</a-button>
        </template>

        <span slot="action" slot-scope="text, record">
          <template>
            <a class="m-r-10" @click="handleOperation('addChild', record)">新增子部门</a>
            <a class="m-r-10" @click="handleOperation('update',record)">修改</a>
            <a-popconfirm
              title="您确定是否删除当前部门?"
              ok-text="是"
              cancel-text="否"
              placement="bottomLeft"
              @confirm="handleRemove(record.ID)"
            >
              <a-icon slot="icon" type="question-circle-o" />
              <a href="#">删除</a>
            </a-popconfirm>
          </template>
        </span>
      </s-table>
    </a-card>
    <edit-department :visible.sync="visibles" :propsData="propsData"></edit-department>
  </page-header-wrapper>
</template>

<script>
import { STable } from '@/components'
import { getDepartmentList, delDepartments } from '@/api/system'
import EditDepartment from './edit'
const columns = [
  {
    title: '部门名称',
    align: 'left',
    dataIndex: 'DepartmentName'
  },
  {
    title: '部门等级',
    align: 'center',
    dataIndex: 'BMDJMC'
  },
  {
    title: '部门地址',
    align: 'center',
    dataIndex: 'BBDZ'
  },
  {
    title: '部门电话',
    align: 'center',
    dataIndex: 'BBDH'
  },
  {
    title: '操作',
    align: 'center',
    width: '200px',
    fixed: 'right',
    scopedSlots: { customRender: 'action' }
  }
]

export default {
  name: 'DepartmentList',
  components: {
    STable,
    EditDepartment
  },
  data () {
    this.columns = columns
    return {
      visibles: false,
      // 查询参数
      queryParam: {
        DepartmentName: '',
        userId: this.$store.getters.loginName
      },
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        return getDepartmentList(Object.assign(parameter, this.queryParam)).then(res => {
          return res
        })
      },
      propsData: {
        type: 'add',
        id: ''
      }
    }
  },
  methods: {
    handleOperation (type, record) {
      this.visibles = true
      this.propsData = { type, record }
    },
    handleRemove (id) {
      delDepartments({ id }).then((res) => {
        if (res.code) {
          this.$message.success('部门删除成功', 2.5)
        } else {
          this.$message.error('部门删除失败, 请稍后重试', 2.5)
        }
      }).catch((err) => {
        this.$message.error(err, 2.5)
      }).finally(() => {
        this.$refs.table.refresh(true)
      })
    },
    handleReset () {
      this.queryParam = {
        DepartmentName: '',
        userId: this.$store.getters.loginName
      }
      this.$refs.table.refresh()
    }
  }
}
</script>
